package com.system.enterprisemanagementsystem.mapper;

import com.system.enterprisemanagementsystem.pojo.Emp;
import com.system.enterprisemanagementsystem.pojo.EmpQueryParam;
import org.apache.ibatis.annotations.*;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

@Mapper
public interface EmpMapper {


    /**
     * 根据用户名和密码查询用户信息
     * @param username
     * @param password
     * @return
     */
    @Select("SELECT * FROM emp WHERE username = #{username} AND password = #{password}")
    Emp findByUsernameAndPassword(String username, String password);

    /**
     * 查询所有的员工数据，及其关联的部门名称
     */
    @Select("SELECT e.id, e.username, e.name, e.gender, e.position, e.dept_id as deptId, " +
            "d.name as deptName, e.avatar, e.phone, e.salary, e.hire_date as hireDate, " +
            "e.create_time as createTime, e.update_time as updateTime " +
            "FROM employee e LEFT JOIN dept d ON e.dept_id = d.id ORDER BY e.id")
    List<Emp> selectAllWithDeptName();




//    /** 没用分页插件
//     * 查询总记录数
//     */
//    @Select("select count(*) from employee e left join dept d on e.dept_id = d.id ")
//    public Long count();
//
//    /**
//     * 查询所有的员工及其对应的部门名称
//     */
//    @Select("select e.*, d.name deptName from employee as e left join dept as d on e.dept_id = d.id " +
//            "limit #{start}, #{pageSize}")
//    public List<Emp> list(Integer start , Integer pageSize);

//    /** 用了分页插件之后
//     * 查询所有的员工及其对应的部门名称
//     */
//    @Select("select e.*, d.name deptName from employee as e left join dept as d on e.dept_id = d.id")
//    public List<Emp> list();


    /**
            * 查询所有的员工及其对应的部门名称
     */
    List<Emp> list(EmpQueryParam empQueryParam);

    /**
     * 根据部门id查询对应的员工数量
     * @param deptId
     * @return
     */
    @Select("SELECT COUNT(*) FROM emp WHERE dept_id = #{deptId}")
    Long countByDeptId(Integer deptId);

    /**
     * 查询所有的员工
     * @return
     */
    @Select("SELECT * from emp")
    List<Emp> getAllEmp();
    /**
     * 新增员工数据
     */
    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into emp(username, name, gender, phone, job, salary, image, entry_date, dept_id, create_time, update_time) " +
            "values (#{username},#{name},#{gender},#{phone},#{job},#{salary},#{image},#{entryDate},#{deptId},#{createTime},#{updateTime})")
    void insert(Emp emp);

    /**
     * 删除员工信息
     * @param ids
     */
    void deleteByIds(List<Integer> ids);

    /**
     * 根据员工id查询
     * @param id
     * @return
     */
    Emp queryById(Integer id);

    /**
     * 修改员工数据
     * @param emp
     */
    void updateEmp(Emp emp);

    /**
     * 统计各个职位的员工人数
     */
    @MapKey("pos")
    List<Map<String,Object>> countEmpJobData();

    /**
     * 统计员工性别信息
     */
    @MapKey("name")
    List<Map> countEmpGenderData();
}
